package com.min.main.controller;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import com.min.common.entity.BaseController;
import com.min.common.entity.R;
import com.min.common.util.HttpContextUtils;
import com.min.common.util.ShiroUtils;
import com.min.main.entity.UserEntity;
import com.min.main.form.LoginForm;
import com.min.main.service.CaptchaService;
import com.min.main.service.RoleService;
import com.min.main.service.RoleUserService;
import com.min.main.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 用户表
 *
 * @author fengt
 * @date 2021-06-23 11:41:08
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController extends BaseController<UserEntity, UserService> {

    @Autowired
    CaptchaService captchaService;

    @Autowired
    RoleUserService roleUserService;

    @Autowired
    RoleService roleService;

    /**
     * 获取验证码
     * @param uuid 验证码唯一标识
     * @return: void
     * @author: fengt
     * @date: 2021/6/23 11:50
     **/
    @GetMapping("/captcha.jpg")
    public void captcha(String uuid) throws IOException {

        if (StrUtil.isBlank(uuid)) {
            log.error("获取验证码图片失败，UUID为NULL");
            return;
        }

        HttpServletResponse response = HttpContextUtils.getResponse();
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");

        ServletOutputStream out = response.getOutputStream();
        captchaService.getCaptcha(uuid,out);
        IoUtil.close(out);
    }

    /**
     * 用户登录
     * @param loginForm {@link com.min.main.form.LoginForm }
     * @return: com.min.common.entity.R
     * @author: fengt
     * @date: 2021/6/24 14:35
     **/
    @PostMapping("/login")
    public R login(@RequestBody LoginForm loginForm) {
        return this.service.login(loginForm);
    }

    /**
     * 用户注销
     * @return: com.qd.platform.common.utils.R
     * @author: fengt
     * @date: 2020-06-28 17:13
     **/
    @PostMapping("/logout")
    public R logout() {
        if (ShiroUtils.getUser() != null) {
            this.service.logout(ShiroUtils.getUser().getToken());
        }
        return R.ok();
    }

    @Override
    public R one(@PathVariable("id") Long id) {
        final UserEntity user = service.getById(id);
        final Long currentUserId = ShiroUtils.getUser().getId();
        List<Long> roleIds = roleUserService.getRoleIds(currentUserId);
        user.setRoleIdList(roleIds);
        return R.ok().put("data", user);
    }

    @Override
    public R add(@RequestBody UserEntity u) {
        return this.service.saveOrUpdateWithRoles(u) ? R.ok() : R.error();
    }

    @Override
    public R edit(@RequestBody UserEntity u) {
        return this.service.saveOrUpdateWithRoles(u) ? R.ok() : R.error();
    }

}
